//
//  LGCancerPatientBasicController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/19.
//  癌症 -- 病史信息

#import "LGCancerPatientBasicController.h"
#import "LGCancerPatientBasicHeader.h"
#import "LGCancerPatientBasicCell.h"
#import "LGCustomTextView.h"
@interface LGCancerPatientBasicController ()<UITableViewDelegate,UITableViewDataSource,LGTextViewDelegate>
/**   */
@property (nonatomic , strong)LGCancerPatientBasicHeader *headerView;
/**   */
@property (nonatomic , strong)LGCustomTextView *textView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *footerView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , assign)CGFloat textViewH;

@end

@implementation LGCancerPatientBasicController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"病史信息";
    self.textViewH = viewPix(100);
    [self.view addSubview:self.tableView];
    [self requestData];
    //基本信息
    NSDictionary *basicDic = @{@"title":@"基本信息",@"key":@"base",@"items":@[@{@"title":@"出生日期",@"key":@"birthday"},@{@"title":@"手机号",@"key":@"mobile"},@{@"title":@"身份证号",@"key":@"card_no"}]};
    //社会信息
    NSDictionary *societyDic = @{@"title":@"社会信息",@"key":@"renkx",@"items":@[@{@"title":@"常居地",@"key":@"address"},@{@"title":@"婚姻状况",@"key":@"married"},@{@"title":@"文化程度",@"key":@"education"},@{@"title":@"职业",@"key":@"profession"}]};
    
    //病史信息:肺癌、食管癌
    NSDictionary *diseaseDic = @{@"title":@"病史信息",@"key":@"jbxx",@"items":@[@{@"title":@"病理分型",@"key":@"blfx_fa"},@{@"title":@"转移",@"key":@"zhuany_fa"},@{@"title":@"临床分期",@"key":@"linchuanfq_fa"},@{@"title":@"T分期",@"key":@"tfengqi"},@{@"title":@"N分期",@"key":@"nfengqi"},@{@"title":@"M分期",@"key":@"mfengqi"},@{@"title":@"G分级",@"key":@"gfengqi"},@{@"title":@"确诊时间",@"key":@"quezhensj"},@{@"title":@"您的手术史?",@"key":@"shouss_fa"},@{@"title":@"您的疾病史？",@"key":@"jibings_fa"},@{@"title":@"您的过敏史？",@"key":@"guomings"}]};
    //病史信息:肺结节
    NSDictionary *diseaseDic2 = @{@"title":@"病史信息",@"key":@"jbxx",@"items":@[@{@"title":@"目前症状",@"key":@"blfx_fa"},@{@"title":@"当前结节类型",@"key":@"linchuanfq_fa"},@{@"title":@"您的疾病史？",@"key":@"jibings_fa"},@{@"title":@"您的手术史？",@"key":@"shouss_fa"},@{@"title":@"您的过敏史？",@"key":@"guomings"},@{@"title":@"您的吸烟情况？",@"key":@"smoking"},@{@"title":@"您家中的做饭燃料？",@"key":@"rangqi"},@{@"title":@"您平时体力活动情况？（体力活动是指至少30分钟以上体力活动）",@"key":@"tilihuod"}]};
    //个人史
    NSDictionary *percentDic = @{@"title":@"个人史",@"key":@"jbxx",@"items":@[@{@"title":@"您的吸烟情况？",@"key":@"smoking"}]};
    //家族史
    NSDictionary *familyDic = @{@"title":@"家族史",@"key":@"jbxx",@"items":@[@{@"title":@"是否有恶性肿瘤家族史？",@"key":@"jiazushi_fa"}]};
    if ([self.diseaseCode isEqualToString:@"3"]) {
        //肺结节
        self.dataArray = @[basicDic,societyDic,diseaseDic2];
    }else{
        //肺癌、食管癌
        self.dataArray = @[basicDic,societyDic,diseaseDic,percentDic,familyDic];
    }
    [self.tableView reloadData];
}
-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.profile/detailV2" parameters:@{@"patient_id":self.patientid,@"disease_code":self.diseaseCode} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.headerView.dataDic = returnData[@"base"];
            self.headerView.diagnose = [returnData[@"jbxx"][@"blfx_fa"] isKindOfClass:[NSString class]]?LGNSString(returnData[@"jbxx"][@"blfx_fa"]):@"";
            self.headerView.blfx = LGNSString(returnData[@"jbxx"][@"linchuanfq_fa"]);
            self.headerView.time = LGNSString(returnData[@"jbxx"][@"quezhensj"]);
            self.textView.content = LGNSString(returnData[@"other"][@"bz"]);
            self.dataDic = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)textViewHeightChanged:(CGFloat)height{
    if (height != self.textViewH) {
        CGFloat newHeight = (height > self.textViewH)?height:self.textViewH;
        self.textView.frame = CGRectMake(viewPix(25), viewPix(45), Screen_W-viewPix(50), newHeight);
        self.footerView.frame = CGRectMake(0, 0, Screen_W, newHeight+viewPix(60));
        self.tableView.tableFooterView = self.footerView;
        self.textViewH = newHeight;
    }
}

-(void)textViewDidEndEditing:(NSString *)content{
    [RequestUtil withPOST:@"/doctor/patient.HealthRecords/savebz" parameters:@{@"patient_id":self.patientid , @"content":content} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code != 200) {
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGCancerPatientBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *tempDic = self.dataArray[indexPath.section][@"items"][indexPath.row];
    NSString *key = LGNSString(tempDic[@"key"]);
    cell.title = LGNSString(tempDic[@"title"]);
    NSString *content = @"";
    NSString *superKey = LGNSString(self.dataArray[indexPath.section][@"key"]);
    if ([self.dataDic isKindOfClass:[NSDictionary class]] && [self.dataDic.allKeys containsObject:superKey]) {
        NSDictionary *superDic = self.dataDic[superKey];
        if ([superDic isKindOfClass:[NSDictionary class]] && [superDic.allKeys containsObject:key]) {
            if ([superDic[key] isKindOfClass:[NSArray class]]) {
                //内容是个数组
                NSArray *itemArray = superDic[key];
                if ([key isEqualToString:@"shouss_fa"]) {
                    //手术史是个对象数组
                    for (NSDictionary *contentDic in itemArray) {
                        content = (content.length>0)?[NSString stringWithFormat:@"%@、%@ %@",content,contentDic[@"title"],contentDic[@"date"]]:[NSString stringWithFormat:@"%@ %@",contentDic[@"title"],contentDic[@"date"]];
                    }
                }else{
                    //字符串数组
                    for (NSString *contentStr in itemArray) {
                        content = (content.length>0)?[NSString stringWithFormat:@"%@、%@",content,contentStr]:contentStr;
                    }
                }
            }else{
                //内容是字符串
                content = LGNSString(superDic[key]);
            }
        }
        cell.content = content;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(45);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(45))];
    headView.topMargin = viewPix(8);
    UILabel *titleLabel = [UILabel lableWithText:self.dataArray[section][@"title"] colorString:@"#000000" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(28), viewPix(20), Screen_W-viewPix(56), viewPix(25));
    [headView addSubview:titleLabel];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footerView.bottomMargin = viewPix(8);
    return footerView;
}
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGCancerPatientBasicCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

-(LGCancerPatientBasicHeader *)headerView{
    if(!_headerView){
        _headerView = [[LGCancerPatientBasicHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(92))];
    }
    return _headerView;
}

-(UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(160))];
        UIView *baseView = [[UIView alloc]init];
        baseView.backgroundColor = [UIColor whiteColor];
        baseView.cornerRidus = 10;
        [_footerView addSubview:baseView];
        [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_footerView).offset(viewPix(8));
            make.bottom.equalTo(_footerView).offset(-viewPix(8));
            make.left.equalTo(_footerView).offset(viewPix(16));
            make.right.equalTo(_footerView).offset(-viewPix(16));
        }];
        
        UILabel *titleLabel = [UILabel lableWithText:@"特殊备注" colorString:@"#000000" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        titleLabel.frame = CGRectMake(viewPix(28), viewPix(20), Screen_W-viewPix(56), viewPix(25));
        [_footerView addSubview:titleLabel];
        [_footerView addSubview:self.textView];
    }
    return _footerView;
}

-(LGCustomTextView *)textView{
    if(!_textView){
        _textView = [LGCustomTextView textViewWithPlaceHolder:@"请输入" textColor:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft keyboard:UIKeyboardTypeDefault];
        _textView.frame = CGRectMake(viewPix(25), viewPix(45), Screen_W-viewPix(50), viewPix(100));
        _textView.customDelegate = self;
    }
    return _textView;
}

@end
